package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CategoryMenuMapper;
import com.itheima.pojo.CategoryMenu;
import com.itheima.service.CategoryMenuService;
import com.itheima.utils.IdWorker;
import com.itheima.utils.Result;
import com.itheima.utils.StatusCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CategoryMenuServiceImpl implements CategoryMenuService {

    @Autowired
    private CategoryMenuMapper categoryMenuMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public int add(CategoryMenu categoryMenu) {

        if (categoryMenu.getMenuLevel()==1 && categoryMenu.getParentUid()!= ""){
            return 0;
        }
        categoryMenu.setUid(String.valueOf(idWorker.nextId()));
        categoryMenu.setCreateTime(new Date());
        categoryMenu.setUpdateTime(new Date());

        int i = categoryMenuMapper.insert(categoryMenu);
        return i;
    }


    @Override
    public Result delete(CategoryMenu categoryMenu) {
        //1查询是否有子菜单，如果存在则无法删除
        int count = categoryMenuMapper.count(categoryMenu.getUid());

        if (count>0){
         return new Result<>(StatusCode.FAIL,"有子菜单无法删除");
        }
        categoryMenuMapper.delete(categoryMenu);
        return new Result<>(StatusCode.SUCCESS,"删除成功");

    }

    @Override
    public Result edit(CategoryMenu categoryMenu) {
        CategoryMenu categoryMenuMapperById = categoryMenuMapper.findById(categoryMenu.getUid());
        if (categoryMenuMapperById==null){
            return new Result<>(StatusCode.FAIL,"没有数据，无法修改");
        }
        categoryMenu.setUpdateTime(new Date());
        categoryMenuMapper.updateByPrimaryKeySelective(categoryMenu);
        return new Result<>(StatusCode.SUCCESS,"编辑成功");
    }


    @Override
    public List<CategoryMenu> getAll(String keyword) {
        /*if (StringUtils.isNotEmpty(keyword)){
            return null;
        }*/
        //查询级别为1的菜单
        CategoryMenu categoryMenu = new CategoryMenu();
        categoryMenu.setMenuLevel(1);
        List<CategoryMenu> categoryMenuList = categoryMenuMapper.select(categoryMenu);

        //获取级别为1的菜单所有的id，去查询下一级别的菜单
       List<String> ids = new ArrayList<>();
        categoryMenuList.forEach(item ->{
            if (StringUtils.isNotEmpty(item.getUid())){
                ids.add(item.getUid());
            }
        });


        List<CategoryMenu> categoryMenuList2 = categoryMenuMapper.selectInIds(ids);
       // System.out.println("cttttt--:"+categoryMenuList2);

        //获取级别为2的菜单所有的id，去查询下一级别的菜单
        List<String> secondIds = new ArrayList<>();
        categoryMenuList2.forEach(item ->{
            if (StringUtils.isNotEmpty(item.getUid())){
                secondIds.add(item.getUid());
            }
        });

        List<CategoryMenu> categoryMenuList3 = categoryMenuMapper.selectInIds(secondIds);


        Map<String, List<CategoryMenu>> map = new HashMap<>();
        categoryMenuList3.forEach(item ->{
            if (StringUtils.isNotEmpty(item.getParentUid())){
                if (map.get(item.getParentUid())==null){
                    List<CategoryMenu> list = new ArrayList<>();
                    list.add(item);
                    map.put(item.getParentUid(),list);
                }else {
                    List<CategoryMenu> list = map.get(item.getParentUid());
                    list.add(item);
                    map.put(item.getParentUid(),list);
                }

            }
        });

       //向级别2中添加级别3的数据
        categoryMenuList2.forEach(item ->{
            if (map.get(item.getUid())!=null){
                List<CategoryMenu> list = map.get(item.getUid());
              Collections.sort(list, new Comparator<CategoryMenu>() {
                  @Override
                  public int compare(CategoryMenu o1, CategoryMenu o2) {
                      return o1.getSort()<o2.getSort()?1:-1;
                  }
              });
              item.setChildCategoryMenu(list);
            }
        });

        //向级别1中添加级别2的数据
        for (CategoryMenu menu : categoryMenuList) {
           List<CategoryMenu> list = new ArrayList<>();
            for (CategoryMenu item : categoryMenuList2) {
                if (item.getParentUid().equals(menu.getUid())){
                    list.add(item);
                }
            }
            Collections.sort(list, new Comparator<CategoryMenu>() {
                @Override
                public int compare(CategoryMenu o1, CategoryMenu o2) {
                    return o1.getSort()<o2.getSort()?1:-1;
                }
            });
            menu.setChildCategoryMenu(list);

        }

        categoryMenuList.sort(new Comparator<CategoryMenu>() {
            @Override
            public int compare(CategoryMenu o1, CategoryMenu o2) {
                return o1.getSort()<o2.getSort()?1:-1;
            }
        });
        return categoryMenuList;
    }


    //获取所有二级菜单-按钮列表
    @Override
    public List<CategoryMenu> getButtonAll(String keyword) {

        //查询级别为2的菜单
        CategoryMenu categoryMenu = new CategoryMenu();
        if (StringUtils.isNotEmpty(keyword)){
            categoryMenu.setUid(keyword);
        }else {
            categoryMenu.setMenuLevel(2);
        }
        List<CategoryMenu> categoryMenuList = categoryMenuMapper.select(categoryMenu);

        //获取级别为2的菜单所有的id，去查询下一级别的菜单
        List<String> ids = new ArrayList<>();
        categoryMenuList.forEach(item ->{
            if (StringUtils.isNotEmpty(item.getUid())){
                ids.add(item.getUid());
            }
        });

        List<CategoryMenu> categoryMenuList1 = categoryMenuMapper.selectInIds(ids);
        Set<String> secondUidSet = new HashSet<>();
        Map<String, List<CategoryMenu>> map = new HashMap<>();
        categoryMenuList1.forEach(item ->{
            if (StringUtils.isNotEmpty(item.getParentUid())){
                secondUidSet.add(item.getParentUid());
                if (map.get(item.getParentUid())==null){
                    List<CategoryMenu> list = new ArrayList<>();
                    list.add(item);
                    map.put(item.getParentUid(),list);
                }else {
                    List<CategoryMenu> list = map.get(item.getParentUid());
                    list.add(item);
                    map.put(item.getParentUid(),list);
                }
            }
        });

        //过滤不是按钮的级别
        List<CategoryMenu> list = new ArrayList<>();
        for (CategoryMenu menu : categoryMenuList) {
            for (String uid : secondUidSet) {
                if (menu.getUid().equals(uid)){
                    list.add(menu);
                    break;
                }
            }
        }

        //向二级菜单中添加三级数据
        list.forEach(item->{
            if (map.get(item.getUid())!= null){
                List<CategoryMenu> menuList = map.get(item.getUid());
                Collections.sort(menuList, new Comparator<CategoryMenu>() {
                    @Override
                    public int compare(CategoryMenu o1, CategoryMenu o2) {
                        return o1.getSort()<o2.getSort()?1:-1;
                    }
                });

                item.setChildCategoryMenu(menuList);

            }
        });

        return list;
    }

    //获取菜单列表
    @Override
    public Page<CategoryMenu> getList(CategoryMenu categoryMenu) {
        PageHelper.startPage(categoryMenu.getCurrentPage(),categoryMenu.getPageSize());

        if (categoryMenu.getMenuLevel()==null){
            return null;
        }
        List<CategoryMenu> menuList = categoryMenuMapper.select(categoryMenu);

        return (Page<CategoryMenu>) menuList;
    }

    //置顶
    @Override
    public int stick(CategoryMenu categoryMenu) {
        CategoryMenu menu = categoryMenuMapper.findById(categoryMenu.getUid());

        System.out.println("menu----:"+menu);
        //根据级别的等级查询本等级sort的最大者
       String count =  categoryMenuMapper.selectMax(menu.getMenuLevel());

        System.out.println("count-----:"+count);

       //将最大的sort+1赋值给本sort，再修改数据库
        categoryMenu.setSort(Integer.parseInt(count)+1);
        categoryMenu.setUpdateTime(new Date());
        int i = categoryMenuMapper.updateByPrimaryKeySelective(categoryMenu);
        return i;
    }


}
